//
// Mouse Gesture Recognizer
// (c) Robert Hahn / HTW Dresden
//
// NetworkNeural.cs
// ----------------
// Base class for neural networks.
//


using System;
using System.ComponentModel;


namespace CNeural
{
	/// <summary>
	/// Represents an event data container for neural networks.
	/// </summary>
	public class NetworkNeuralEventArgs : EventArgs 
	{
		#region Fields
		private int     learnStep;
		private double  currentError;
		#endregion
		
		#region Properties
		/// <summary>
		/// Gets or sets the current learn step.
		/// </summary>
		public int LearnStep 
		{
			get { return this.learnStep; }
			set { this.learnStep = value; }
		}

		/// <summary>
		/// Gets or sets the current error of the network.
		/// </summary>
		public double CurrentError
		{
			get { return this.currentError; }
			set { this.currentError = value; }
		}
		#endregion

		#region Constructors
		public NetworkNeuralEventArgs(int learnStep, double currentError) 
		{
			this.learnStep = learnStep;
			this.currentError = currentError;
		}
		#endregion
	}

	
	/// <summary>
	/// Abstract base class for neural networks.
	/// </summary>
	[Serializable]
	[TypeConverter(typeof(ExpandableObjectConverter))]
	public abstract class NetworkNeural
	{
		#region Fields
		protected Random   randomGenerator;
		protected double   learnRate;
		protected int      maximumIterations;
		protected int      currentIteration;
		protected double   errorLimit;
		protected double   currentError;
		protected bool     isConnected;
		#endregion
		
		#region Events
		
		public virtual event EventHandler LearningFinished ;
		public virtual event EventHandler StepFinished;
		
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the learn rate of the training algorithm.
		/// </summary>
		[Description("The learn rate of the training algorithm.")]
		public double LearnRate 
		{
			get { return this.learnRate; }
			set { this.learnRate = value; }
		}

		/// <summary>
		/// Gets or set the maximum number of learning iterations.
		/// </summary>
		[Description("The maximum number of learning iterations.")]
		public int MaximumIterations 
		{
			get { return this.maximumIterations; }
			set { this.maximumIterations = value; }
		}

		/// <summary>
		/// Gets or sets the maximum acceptable error of the network. This value 
		/// is used in the learning algorithm as abort condition.
		/// </summary>
		[Description("The maximum acceptable error of the network.")]
		public double ErrorLimit
		{
			get { return this.errorLimit; }
			set { this.errorLimit = value; }
		}

		/// <summary>
		/// Gets the current error of the network.
		/// </summary>
		[Description("The current error of the network.")]
		public double CurrentError 
		{
			get { return this.currentError; }
		}

		/// <summary>
		/// Gets the current learning iteration.
		/// </summary>
		[Browsable(false)]
		public int LearnIterations 
		{
			get { return this.currentIteration; }
		}


		/// <summary>
		/// Determines if the neurons of the network are connected.
		/// </summary>
		[Description("Determines if the neurons are connected.")]
		public bool IsConnected 
		{
			get { return this.isConnected; }
			set { this.isConnected = value; }
		}
		#endregion

		#region Methods
		public abstract void Reset();
		#endregion

		#region Constructor
		public NetworkNeural()
		{
			randomGenerator        = new Random(DateTime.Now.Millisecond);
			this.maximumIterations = 1000;
			this.currentIteration  = 0;
			this.errorLimit        = 0.001F;
			this.learnRate         = 0.5F;
			this.isConnected       = false;
			this.LearningFinished  = null;
			this.StepFinished      = null;
		}
		#endregion
	}
}
